﻿using System;
using Autodesk.Revit.DB;

namespace YArchitech.Plumbing
{
	public class XYZMath
	{
		public static XYZ FindMidPoint(XYZ first, XYZ second)
		{
			double num = (first.X + second.X) / 2.0;
			double num2 = (first.Y + second.Y) / 2.0;
			double num3 = (first.Z + second.Z) / 2.0;
			return new XYZ(num, num2, num3);
		}

		public static double FindDistance(XYZ first, XYZ second)
		{
			double num = first.X - second.X;
			double num2 = first.Y - second.Y;
			double num3 = first.Z - second.Z;
			return Math.Sqrt(num * num + num2 * num2 + num3 * num3);
		}

		public static XYZ FindDirection(XYZ first, XYZ second, XYZ vector)
		{
			double num = second.X - first.X;
			double num2 = second.Y - first.Y;
			double num3 = second.Z - first.Z;
			double num4 = XYZMath.FindDistance(first, second);
			XYZ result = new XYZ(0.0, 0.0, 0.0);
			if ((vector.Y >= 0.0 && num >= 0.0) || (vector.Y <= 0.0 && num <= 0.0))
			{
				result = new XYZ(-num2 / num4, num / num4, num3 / num4);
			}
			else if ((vector.Y <= 0.0 && num >= 0.0) || (vector.Y >= 0.0 && num <= 0.0))
			{
				result = new XYZ(num2 / num4, -num / num4, num3 / num4);
			}
			return result;
		}

		public static XYZ GetDirectionVector(XYZ first, XYZ second)
		{
			double num = second.X - first.X;
			double num2 = second.Y - first.Y;
			double num3 = second.Z - first.Z;
			double num4 = XYZMath.FindDistance(first, second);
			new XYZ(0.0, 0.0, 0.0);
			return new XYZ(num / num4, num2 / num4, num3 / num4);
		}

		public static XYZ FindRightDirection(XYZ viewDirection)
		{
			double num = -viewDirection.Y;
			double x = viewDirection.X;
			double z = viewDirection.Z;
			return new XYZ(num, x, z);
		}

		public static XYZ FindUpDirection()
		{
			return new XYZ(0.0, 0.0, 1.0);
		}

		private const double PRECISION = 1E-10;
	}
}
